package com.ossjk.qlh.dishes.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.core.vo.RequestBean;
import com.ossjk.core.vo.ResponseBean;
import com.ossjk.qlh.business.vo.MenuVO;
import com.ossjk.qlh.business.vo.SetmealVO;
import com.ossjk.qlh.dishes.entity.Menu;
import com.ossjk.qlh.dishes.service.IMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;

/**
 *  
 * 菜单
 * @author liuxi
 * @since 2020-08-15
 */
@Api(tags = "菜单")
@RestController
@RequestMapping("/dishes/menu")
public class MenuController extends BaseController {
	
	@Autowired
	private IMenuService iMenuService;

	@ApiOperation(value = "列表")
	@RequiresPermissions("00100020")
	@PostMapping("/list")
	public ResponseBean<Page<MenuVO>> list(@RequestBody RequestBean requestBean) {
		QueryWrapper<MenuVO> queryWrapper = new QueryWrapper<MenuVO>();
		if (!CommonUtil.isBlank(requestBean.getParam().get("type"))) {
			queryWrapper.like("m.type", requestBean.getParam().get("type"));
		}
		if (!CommonUtil.isBlank(requestBean.getParam().get("ename"))) {
			queryWrapper.like("e.ename", requestBean.getParam().get("ename"));
		}
		if (!CommonUtil.isBlank(requestBean.getParam().get("edate"))) {
			queryWrapper.like("m.edate", requestBean.getParam().get("edate"));
		}
		if (!CommonUtil.isBlank(requestBean.getParam().get("eid"))) {
			queryWrapper.like("m.eid", requestBean.getParam().get("eid"));
		}
		return responseSuccess(iMenuService.findMenuAll(requestBean.getPage(),queryWrapper));
	}

	@ApiOperation(value = "去修改")
	@RequiresPermissions("001000200010")
	@PostMapping("/toUpdate")
	public ResponseBean<Menu> toUpdate(@RequestBody RequestBean requestBean) {
		Menu menu = iMenuService.getById((Serializable) requestBean.getParam().get("id"));
		if (!CommonUtil.isBlank(menu)) {
			return responseSuccess(menu);
		} else {
			return responseFail();
		}
	}

	@ApiOperation(value = "添加")
	@RequiresPermissions("001000200000")
	@ApiOperationSupport(ignoreParameters = { "id", "crer", "mder", "crtm", "mdtm" })
	@PostMapping("/save")
	public ResponseBean save(@RequestBody RequestBean<Menu> requestBean) {
		if (iMenuService.save(requestBean.getData())) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@ApiOperation(value = "修改")
	@RequiresPermissions("001000200010")
	@ApiOperationSupport(ignoreParameters = { "crer", "mder", "crtm", "mdtm" })
	@PostMapping("/update")
	public ResponseBean update(@RequestBody RequestBean<Menu> requestBean) {
		if (iMenuService.updateById(requestBean.getData())) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@ApiOperation(value = "删除")
	@RequiresPermissions("001000200020")
	@PostMapping("/remove")
	public ResponseBean delete(@RequestBody RequestBean requestBean) {
		List ids = (List) requestBean.getParam().get("ids");
		if (iMenuService.removeByIds(ids)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}
}
